<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
/*云信客户端客户管理*/
class YunXin extends AdminBase{
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();  
    }
	function index(){
		echo $this->fetch();
	}	
	function add(){
		$this->assign("shops",db("shop")->where("parent_id>0")->select());
		echo $this->fetch();
	}
	/*新增云信客户端*/
	function add_post(){
		if(request()->isPost()){
			$accid=input("accid");
			$brakemachine_no=input("brakemachine_no");
			$machine=db("GuestClient")->where("accid='$accid'")->find();
			if(empty($machine)){
				$result=action("api/YunxinApi/createUserId",[$accid]);
				if($result['flag']){
					db("GuestClient")->insert(array(
										"accid"=>$accid,
										"token"=>md5($accid),
										"shop_id"=>input("shop_id"),
										//"start_date"=>input("start_date"),
										//"end_date"=>input("end_date"),
										"status"=>1,
										"descs"=>input("descs"),
										"brakemachine_no"=>input("brakemachine_no")
									));
					$this->ajax_success("成功!");	
				}else{
					$this->ajax_error($result['msg']);
				}
			}else{
				$this->ajax_error("accid 存在");
			}
		}else{
			$this->ajax_error("非法操作!");
		}
	}
	/*修改云信客户端信息*/
	function edit(){
		$id=input("id");
		echo $this->fetch();			
	}
	function edit_post(){
		$this->ajax_success("成功！");	
	}
	/*推送测试*/
	function push(){
		$accid=db("GuestClient")->where("id=".input("id"))->value("accid");
		$result=action("api/YunxinApi/sendAttachMsg",[$accid,0]);
	}
	function delete(){
		if(request()->isPost()){
			$this->ajax_success("成功");
		}else{
			$this->ajax_error("非法操作！");
		}
	}
	/*远程开门*/
	function open(){
		$client=db("GuestClient")->where("id=".input("id"))->find();
		if($client['is_line']!=1){
			$this->ajax_error("设备不在线");
		}
		$url="http://".$_SERVER['HTTP_HOST']."/api/YunxinApi/OpenDoor";
		$time=time();
		$token=$client['token'];
		$result= post_json($url, array("token"=>$token,"time"=>$time,"brakemachine_no"=>$client['brakemachine_no'],"sign"=>md5($token."_".$time)));
		$this->ajax_success("开门结果！",$result);
	}
	
	/*云信客户端列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10001003");
		$this->assign("menus",$auth_group);
	}
	
	/*获取云信客户端数据*/
	private function _ajax_index(){
		$query=array('accid'=>array("field" =>"a.accid","operator" =>"="),
					 );
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		$total=db("GuestClient")->alias("a")->join([["m_shop b","a.shop_id=b.id","LEFT"]])->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("GuestClient")->field("a.*,b.shop_name")->alias("a")->join([["m_shop b","a.shop_id=b.id","LEFT"]])->where($where_and)->order($this->get_order("a"))->limit($this->get_limit())->fetchsql(FALSE)->select();
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
}
